package com.qinggeng.project.system.controller;


import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.GiftProbability;
import com.qinggeng.project.system.service.IGiftProbabilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author smallfish
 */
@RestController
@RequestMapping("/system/probability")
public class GiftProbabilityController extends BaseController {

    @Autowired
    private IGiftProbabilityService probabilityService;

    /**
     * 获取奖品中奖率列表
     * @param probability
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:probability:list')")
    @GetMapping("/list")
    public TableDataInfo list(GiftProbability probability){
        List<GiftProbability> giftProbabilityList=probabilityService.selectGiftProbabilityList(probability);
        return getDataTable(giftProbabilityList);
    }

    /**
     * 获取奖品中奖率详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:probability:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(probabilityService.selectGiftProbabilityById(id));
    }

    /**
     * 新增奖品中奖率
     */
    @PreAuthorize("@ss.hasPermi('system:probability:add')")
    @Log(title = "奖品中奖率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GiftProbability probability) {
        this.setProbabilityAndPriceTitle(probability);
        return toAjax(probabilityService.insertGiftProbability(probability));
    }

    /**
     * 修改奖品中奖率
     */
    @PreAuthorize("@ss.hasPermi('system:probability:edit')")
    @Log(title = "奖品中奖率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GiftProbability probability) {
        this.setProbabilityAndPriceTitle(probability);
        return toAjax(probabilityService.updateGiftProbability(probability));
    }

    /**
     * 删除商品赠品
     */
    @PreAuthorize("@ss.hasPermi('system:probability:remove')")
    @Log(title = "奖品中奖率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(probabilityService.deleteGiftProbabilityByIds(ids));
    }

    private GiftProbability setProbabilityAndPriceTitle(GiftProbability probability){

        probability.setFreeProbability(probabilityService.getGiftProbability(probability.getFreeRate()));
        probability.setHalfProbability(probabilityService.getGiftProbability(probability.getHalfRate()));
        probability.setOtherProbability(probabilityService.getGiftProbability(probability.getOtherRate()));

        if(null==probability.getMinPrice()){
            throw new CustomException("最小值不能为空", ShowTypeEnum.用户.getValue());
        }
        StringBuffer title=new StringBuffer(probability.getMinPrice().toString());
        if(null==probability.getMaxPrice()){
            title.append("元以上");
        }
        if(probability.getMaxPrice().compareTo(probability.getMinPrice())<0){
            throw new CustomException("最大值不能小于最小值",ShowTypeEnum.用户.getValue());
        }
        title.append("-").append(probability.getMaxPrice().toString()).append("元");
        probability.setPriceTitle(title.toString());
        return probability;
    }
}
